package com.controller;

import com.dao.AssignmentDao;
import com.model.Assignment;
import com.model.Student;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

@WebServlet("/student/assignment/*")
public class StudentAssignmentServlet extends HttpServlet {
    private AssignmentDao assignmentDao = new AssignmentDao();
    
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) 
            throws ServletException, IOException {
        String pathInfo = req.getPathInfo();
        HttpSession session = req.getSession();
        Student student = (Student) session.getAttribute("user");
        
        System.out.println("=== 学生作业请求 ===");
        System.out.println("路径: " + pathInfo);
        System.out.println("学生信息: " + (student != null ? student.getSname() : "null"));
        
        try {
            if (pathInfo == null || "/".equals(pathInfo) || "/list".equals(pathInfo)) {
                List<Assignment> assignments = assignmentDao.getActiveAssignments();
                req.setAttribute("assignments", assignments);
                req.getRequestDispatcher("/WEB-INF/views/student/assignment.jsp")
                   .forward(req, resp);
            } else if ("/view".equals(pathInfo)) {
                // 查看作业详情
                int aid = Integer.parseInt(req.getParameter("aid"));
                Assignment assignment = assignmentDao.getAssignment(aid);
                
                // 获取学生的提交记录
                Map<String, Object> submission = assignmentDao.getStudentSubmission(aid, student.getSno());
                
                req.setAttribute("assignment", assignment);
                req.setAttribute("submission", submission);
                req.getRequestDispatcher("/WEB-INF/views/student/view.jsp")
                   .forward(req, resp);
            }
        } catch (SQLException e) {
            System.out.println("数据库错误: " + e.getMessage());
            e.printStackTrace();
            throw new ServletException(e);
        }
    }
    
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) 
            throws ServletException, IOException {
        // 设置请求编码为UTF-8
        req.setCharacterEncoding("UTF-8");
        // 设置响应编码为UTF-8
        resp.setCharacterEncoding("UTF-8");
        
        String pathInfo = req.getPathInfo();
        HttpSession session = req.getSession();
        Student student = (Student) session.getAttribute("user");
        
        try {
            if ("/submit".equals(pathInfo)) {
                int aid = Integer.parseInt(req.getParameter("aid"));
                String content = req.getParameter("content");
                
                System.out.println("提交作业内容: " + content); // 添加日志
                
                java.sql.Timestamp submitTime = new java.sql.Timestamp(System.currentTimeMillis());
                assignmentDao.submitAssignment(aid, student.getSno(), content, submitTime);
                
                resp.sendRedirect(req.getContextPath() + "/student/assignment/view?aid=" + aid);
            }
        } catch (SQLException e) {
            throw new ServletException(e);
        }
    }
} 